package com.shop2cn.iapi.agentproducts.testcase;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.GetCatalogInfoByCatalogIdsBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.service.GetCatalogInfoByCatalogIdsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;
/**
*通过规格编辑查询规格信息Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetCatalogInfoByCatalogIds{
	private static GetCatalogInfoByCatalogIdsBean getcataloginfobycatalogidsBean;
	private static GetCatalogInfoByCatalogIdsCall getcataloginfobycatalogidsCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("通过规格编辑查询规格信息Test");
	}

	@Before
	public void caseUp() {
		getcataloginfobycatalogidsBean=new GetCatalogInfoByCatalogIdsBean();
		getcataloginfobycatalogidsCall=new GetCatalogInfoByCatalogIdsCall();
	}
	private static YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getcataloginfobycatalogidsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	// vip渠道
		private static int vipRootUserId = AgentUsers.vipRootUserId;
		private static String vipRootUserName = "NickautoTest";
		private static int csId = AgentUsers.csId;
		private static String csName = "NickautoTest";
		// vip渠道->代理
		private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
		private static String vipAgentUserName1 = "dgsq408897016";
		private static int csId1 = AgentUsers.csId1;
		// vip渠道->代理->代理
		private static int vipAgentUserId2 = AgentUsers.vipAgentUserId2;
		private static String vipAgentUserName2 = "NickautoTest";
		
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogInfoByCatalogIds_001() {
		Logger.start(true,"查询商品条形码，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,
					vipRootUserName, true);
			List<String> barCodes = Arrays.asList("A123_b123_"+TestDataManager.getRandomNum(10),"A123_b123_"+TestDataManager.getRandomNum(10));
			addAgentProductBean.getCatalogList().get(0).setBarcode(barCodes.get(0));;
			addAgentProductBean.getCatalogList().get(1).setBarcode(barCodes.get(1));;
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);

			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService
					.initImportAgentProduct(vipAgentUserId1, vipAgentUserName1, productId, vipRootUserId, true);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);

			ImportAgentProductBean importAgentProductBean1 = AddAgentProductCallService
					.initImportAgentProduct(vipAgentUserId2, vipAgentUserName2, agentProductId, vipAgentUserId1, false);
			String agentProductId1 = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean1);
			
			List<String> catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(agentProductId1).stream().map(ele -> ele.getsCatalogId()).collect(Collectors.toList());
			List<String> Rootcatalogs = ymtCatalogsIWapper.selectCatalogsByProductId(productId).stream().map(ele -> ele.getsCatalogId()).collect(Collectors.toList());
			getcataloginfobycatalogidsBean.setCatalogIds(catalogs);
			getcataloginfobycatalogidsCall.setData(getcataloginfobycatalogidsBean);
			getcataloginfobycatalogidsCall.callService();			
			Logger.verifyEquals("200", getcataloginfobycatalogidsCall.getString("code"), "验证code");
			JsonArray catalog = getcataloginfobycatalogidsCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogs");
			for (int i = 0; i < catalog.size(); i++) {
				JsonObject cata = catalog.get(i).getAsJsonObject();
				Logger.verifyEquals(cata.get("catalogId").getAsString(), catalogs.get(i), "验证规格id");
				Logger.verifyEquals(cata.get("rootCatalogId").getAsString(), Rootcatalogs.get(i), "验证货头规格id");
				Logger.verifyEquals(cata.get("rootProductId").getAsString(),productId, "验证货头商品id");
				Logger.verifyEquals(cata.get("rootUserId").getAsInt(),vipRootUserId, "验证货头id");
				Logger.verifyEquals(cata.get("productName").getAsString(),importAgentProductBean1.getTitle(), "验证商品名称");
				Logger.verifyEquals(cata.get("skuId").getAsString(),importAgentProductBean1.getCatalogList().get(i).getSku(), "验证商品sku");
				Logger.verifyEquals(cata.get("barcode").getAsString(),addAgentProductBean.getCatalogList().get(i).getBarcode(), "验证商品条形码");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}